/**   
* @Title: NettyClientTaskRunLogSend.java 
* @Description: 客户端运行日志推送到服务端
* @author chenmin-ds   
* @date 2017年6月23日 上午11:19:20 
* @version V1.0
*/ 


package cn.com.klun.netty.client;

import cn.com.klun.cache.QuartzConstants;
import cn.com.klun.cache.QuartzPropertiesCache;
import cn.com.klun.cache.QuartzTaskConfigCache;
import cn.com.klun.cache.ServerIpCache;
import cn.com.klun.netty.NettyMessage;
import io.netty.channel.Channel;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/** 
 * @ClassName: NettyClientTaskRunLogSend 
 * @Description: 客户端运行日志推送到服务端
 * @author chenmin-ds 
 * @date 2017年6月23日 上午11:19:20  
 */
public class NettyClientTaskRunLogSend {

	private Logger logger = LoggerFactory.getLogger("NettyClientTaskRunLogSend");
	
	/**
	 * @Title: sendInfoServer
	 * @Description: 推送任务运行日志到服务端
	 * @return void 返回类型
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void sendInfoServer() {
		Map<String, JSONObject> map = null;
		List<JSONObject> list = null;
		try {
			map = QuartzTaskConfigCache.getTaskRunLogArrays();
			if(null == map || map.size() == 0) {
                return;
            }
			
			Channel channel = NettyClientStartup.getInstance().getChannel();
			Iterator iterator = map.entrySet().iterator();
			Entry<String, JSONObject> entry = null;
			String ddlStatus = "";
			list = new ArrayList<JSONObject>();
			while (iterator.hasNext()) {
				entry = (Entry<String, JSONObject>) iterator.next();
				ddlStatus = entry.getValue().getString("ddlStatus");
				if (QuartzConstants.DDL_STATUS_050.equals(ddlStatus) || QuartzConstants.DDL_STATUS_040.equals(ddlStatus)) {
					QuartzTaskConfigCache.clearTaskRunLog(entry.getKey());
				}
				entry.getValue().put("taskRunPort", ServerIpCache.getNettyClientPort());
				list.add(entry.getValue());
			}
			NettyMessage nettyMessage = new NettyMessage(QuartzConstants.TASK_RUN_LOG);
			nettyMessage.setJsonObject("BUSINESS_CODE", QuartzPropertiesCache.getBusinessCode());
			nettyMessage.setJsonObject(QuartzConstants.TASK_RUN_LOG, list);
			channel.writeAndFlush(nettyMessage);
		} catch (Exception e) {
			logger.error("向发服务端发送任务执行日志信息", e);
		} finally {
			if (null != map) {
				map.clear();
			}
			if (null != list) {
				list.clear();
			}
		}
	}
}
